<template>
  <view class="content-wrapper">
    <u-form :model="form" ref="form" label-width="110" error-type="none">
      <u-form-item label="原密码" borderBottom :required="true">
        <u--input v-model="form.oldPassword" type="password" :maxlength="6" clearable placeholder="请填写"
          border="none"></u--input>
      </u-form-item>
      <u-form-item label="新密码" borderBottom :required="true">
        <u--input v-model="form.password" placeholder="请填写" :maxlength="6" type="password" clearable
          border="none"></u--input>
      </u-form-item>
      <u-form-item label="确认新密码" borderBottom :required="true">
        <u--input v-model="form.ackPassword" placeholder="请填写" :maxlength="6" type="password" clearable
          border="none"></u--input>
      </u-form-item>
    </u-form>
    <view class="btn-style" @click="handleSubmit">确认</view>
  </view>
</template>

<script>
  import {
    config
  } from "../../config";
  import {
    postAction
  } from "../../utils/request";
  import {
    Toast,
    navBack
  } from "../../utils/util";
  export default {
    data() {
      return {
        form: {
          oldPassword: "",
          password: "",
          ackPassword: "",
        },
      };
    },
    methods: {
      handleSubmit() {
        function isSixDigitNumber(str) {
          // 使用正则表达式进行匹配
          // \d 表示一个数字，{6} 表示前面的模式恰好重复6次
          // ^ 表示字符串的开始，$ 表示字符串的结束，确保整个字符串都是6位数字
          var regex = /^\d{6}$/;
          return regex.test(str);
        }
        if (!this.form.oldPassword) {
          Toast("请填写原密码！");
          return;
        }
        // 判断是不是 6位数字
        if (!isSixDigitNumber(this.form.oldPassword)) {
          Toast("原密码，请输入6位纯数字的密码！");
          return;
        }

        if (!this.form.password) {
          Toast("请填写新密码！");
          return;
        }
        // 判断新密码是不是 6 位数字
        if (!isSixDigitNumber(this.form.password)) {
          Toast("新密码，请输入6位纯数字的密码！");
          return;
        }
        if (!this.form.ackPassword) {
          Toast("请填写确认新密码！");
          return;
        }
        // 判断两次密码是否一致
        if (this.form.password !== this.form.ackPassword) {
          Toast("两次密码不一致，请检查！");
          return;
        }

        let url = "";
        if (config.role === "fj") {
          url = "/fj/fjUser/upPassword";
        } else {
          url = "/wgy/wgyUser/upPassword";
        }
        postAction(url, {
          ...this.form,
        }).then((res) => {
          Toast("修改成功");
          setTimeout(() => {
            uni.reLaunch({
              url: `/pages/beforeLogin/assistantOfficerLogin`
            })
            // 清除 storage信息
            let role = uni.getStorageSync('role')
            // 
            uni.clearStorageSync()
            uni.setStorageSync('role', role)
          }, 500);
        });
      },
    },
  };
</script>

<style>
  page {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
  }
</style>
<style lang="scss" scoped>
  @import url("../../styles/form.scss");

  .content-wrapper {
    width: 100%;
    height: 100%;
    background: #ffffff;
    margin: 0 auto;
    padding: 0 30rpx;
  }

  .btn-style {
    width: 680rpx;
    height: 88rpx;
    background: #2488f5;
    border-radius: 44rpx 44rpx 44rpx 44rpx;
    border: 1rpx solid #accbec;
    text-align: center;
    margin: 0 auto;
    margin-top: 100rpx;
    font-weight: 500;
    font-size: 36rpx;
    color: #f9f9f9;
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>